package com.chang.soul.controller;

import com.chang.soul.bean.AdminBean;
import com.chang.soul.bean.UserBean;
import com.chang.soul.conf.JwtIgnore;
import com.chang.soul.service.AdminService;
import com.chang.soul.service.UserService;
import com.chang.soul.util.Result;
import com.chang.soul.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/admin")
@Api("管理员管理接口")
public class AdminController {
    @Autowired
    AdminService adminService;

    @ApiOperation(value = "查询所有管理员", notes = "测试用", httpMethod = "GET")
    @GetMapping("/getAllAdmin")
    @JwtIgnore
    public Result getAllUser(){
        return ResultUtil.success(adminService.getAllAdmin());
    }

    @ApiOperation(value = "增加管理员",notes = "",httpMethod = "POST")
    @PostMapping("/insertAdmin")
    @JwtIgnore
    public Result insertAdmin(@Valid @RequestBody AdminBean adminBean){
        System.out.println(adminBean);
        int count = adminService.getCountByNickName(adminBean.getNickName());
        boolean insert_flag= false;
        if(count<1){
            if(insert_flag = adminService.insertAdmin(adminBean)){
                return ResultUtil.success(adminBean);
            }else{
                return ResultUtil.error("insert admin information fail");
            }
        }else{
            return ResultUtil.error("管理员昵称已经存在");
        }
    }

    @ApiOperation(value = "管理员登录",notes = "",httpMethod = "POST")
    @PostMapping("/adminLogin")
    @JwtIgnore
    public Result adminLogin(@RequestParam(value = "nickname")String nickname,
                            @RequestParam(value ="password") String password){
        AdminBean admin = adminService.getAdminByNickName(nickname);
        if(admin == null){
            return ResultUtil.error("管理员昵称为空");
        }
        if(!password.equals(admin.getPassword())){
            return ResultUtil.error("管理员密码错误");
        }else{
            return ResultUtil.success(admin);
        }
    }

    @ApiOperation(value = "删除管理员",notes = "",httpMethod = "GET")
    @GetMapping("/deleteAdminById")
    @JwtIgnore
    public Result deleteAdminById(@RequestParam(value ="id")Integer id){
        boolean deleAd_flag = adminService.deleteAdminById(id);
        if (deleAd_flag){
            return ResultUtil.success("delete admin sucess");
        }else{
            return ResultUtil.error("delete admin fail");
        }
    }

    @ApiOperation(value = "更新管理员",notes = "",httpMethod = "POST")
    @PostMapping("/updateAdmin")
    @JwtIgnore
    public Result updateAdmin(@Valid @RequestBody AdminBean myAdminBean){
        AdminBean adminBean = adminService.getAdminById(myAdminBean.getId()) ;
        String password = myAdminBean.getPassword();

        System.out.println("Password: "+password);
        if(password!= null ){
            adminBean.setPassword(password);
        }
        adminBean.setNickName(myAdminBean.getNickName());
        adminBean.setGender(myAdminBean.getGender());
        adminBean.setAge(myAdminBean.getAge());
        adminBean.setPassword(myAdminBean.getPassword());
        adminBean.setProvince(myAdminBean.getProvince());
        adminBean.setCity(myAdminBean.getCity());
        adminBean.setDirection(myAdminBean.getDirection());
        adminBean.setBrief(myAdminBean.getBrief());

        if(adminService.updateAdmin(adminBean)){
            return ResultUtil.success(adminBean);
        }else{
            return ResultUtil.error("update admin information fail");
        }
    }


    @ApiOperation(value = "通过ID 获取管理员信息",notes = "",httpMethod = "GET")
    @GetMapping("/getAdminById")
    @JwtIgnore
    public Result getAdminById(@RequestParam (value="id")Integer id){
        return ResultUtil.success(adminService.getAdminById(id));
    }

    @ApiOperation(value = "通过城市获取管理员信息接口",notes = "无需token",httpMethod = "GET")
    @GetMapping("/getAdminByCity")
    @JwtIgnore
    public Result getAdminByCity(@RequestParam(value = "city") String city){
        System.out.println(adminService.getAdminByCity(city));
        return ResultUtil.success(adminService.getAdminByCity(city));
    }

}
